#include "effectscene.h"
#include "commondef.h"
#include <QGraphicsPixmapItem>
#include <QGraphicsSceneMouseEvent>
#include "toolkit.h"
#include "commondef.h"
#include "imageitem.h"

enum EffectPressType
{
    NULL_BTP,
    BACK_BTP
}EPressTp(NULL_BTP);

EffectScene::EffectScene(QObject *parent) :
    QGraphicsScene(parent),
    m_selectitem(NULL),
    m_bmove(false),
    m_ioldSelectItem(ToolKit::m_effectTp),
    m_inewSelectItem(ToolKit::m_effectTp)
{
    setSceneRect(0.0, 0.0, 640.0, 360.0);
    initList();

    m_bgItem = new ImageItem(QImage(":/image/BG.png","png"),
                             QRect(QPoint(0,0),QImage(":/image/BG.png","png").size()));
    m_bgItem->setPos(0,0);
    m_bgItem->setZValue(0.0);
    addItem(m_bgItem);

    m_backItem = new ImageItem(QImage(":/image/567.282.png","png"),
                               QRect(QPoint(0,0),QImage(":/image/567.282.png","png").size()));
    m_backItem->setPos(568,282);
    m_backItem->setZValue(2.0);
    addItem(m_backItem);
}

EffectScene::~EffectScene()
{
    if(!m_ItemList.isEmpty())
    {
        foreach(ImageItem* item,m_ItemList)
        {
            if(item != NULL)
            {
                delete item;
                item = NULL;
            }
        }
    }

    m_ItemList.clear();
}

void EffectScene::initList()
{
    for(int i = 0; i < ToolKit::m_effectTp; ++i)
    {
        QString str = QString(":/image/effectbtn/effectbtn (%1).png").arg(i + 1);
        ImageItem* item = new ImageItem(QImage(str,"png"),
                                        QRect(QPoint(0,0),QImage(str,"png").size()));
        item->setPos(EFFECT_START_X - EFFECT_INTERVAL * (ToolKit::m_effectTp - i),EFFECT_NOMAL_Y);
        item->setZValue(1.0);
        addItem(item);
        m_ItemList.append(item);
    }

    QString str = QString(":/image/effectbtn/X%1.png").arg(ToolKit::m_effectTp + 1);
    ImageItem* item = new ImageItem(QImage(str,"png"),
                                    QRect(QPoint(0,0),QImage(str,"png").size()));
    item->setPos(EFFECT_START_X,EFFECT_PRESS_Y);
    item->setZValue(1.0);
    addItem(item);
    m_ItemList.append(item);

    int icount = 1;
    for(int i = ToolKit::m_effectTp + 1; i < EFFECT_NUM; ++i)
    {
        QString str = QString(":/image/effectbtn/effectbtn (%1).png").arg(i + 1);
        ImageItem* item = new ImageItem(QImage(str,"png"),
                                        QRect(QPoint(0,0),QImage(str,"png").size()));
        item->setPos(EFFECT_START_X + icount * EFFECT_INTERVAL,EFFECT_NOMAL_Y);
        item->setZValue(1.0);
        addItem(item);
        ++icount;

        m_ItemList.append(item);
    }
}

void EffectScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(m_backItem->isUnderMouse())
    {
        EPressTp = BACK_BTP;
    }
    else
    {
        EPressTp = NULL_BTP;
        m_startX = event->scenePos().x();

        QList<QGraphicsItem *> itemlist = items(event->scenePos(),Qt::IntersectsItemShape,Qt::DescendingOrder);

        qWarning("%d",itemlist.count());

        if(itemlist.count() == 2)
        {
            m_selectitem = itemlist.at(0);
            m_inewSelectItem = m_ItemList.indexOf((ImageItem*)m_selectitem);

            qWarning("%d",m_inewSelectItem);
        }
    }
}

void EffectScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    switch(EPressTp)
    {
    case BACK_BTP:
    {
        if(m_backItem->isUnderMouse())
            emit back();
    }
        break;
    case NULL_BTP:
    {
        if(m_bmove)
        {
            m_bmove = false;
        }
        else
        {
            QList<QGraphicsItem *> itemlist = items(event->scenePos(),Qt::IntersectsItemShape,Qt::DescendingOrder);
            if(itemlist.count() == 2)
            {
                if(m_selectitem == itemlist.at(0))
                {
                    if(m_inewSelectItem != m_ioldSelectItem)
                    {
                        ImageItem* item = (ImageItem*)m_selectitem;
                        item->setY(EFFECT_PRESS_Y);
                        QString str = QString(":/image/effectbtn/X%1.png").arg(m_inewSelectItem + 1);
                        item->setImage(QImage(str,"png"));

                        m_ItemList.at(m_ioldSelectItem)->setY(EFFECT_NOMAL_Y);
                        str = QString(":/image/effectbtn/effectbtn (%1).png").arg(m_ioldSelectItem + 1);
                        m_ItemList.at(m_ioldSelectItem)->setImage(QImage(str,"png"));

                        m_ioldSelectItem = m_ItemList.indexOf((ImageItem*)m_selectitem);
                        ToolKit::m_effectTp = (EFFECTTP)m_ioldSelectItem;

//                        emit back();
                    }
                }
            }
        }
    }
        break;
    default:
        break;
    }
}

void EffectScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    qreal imove = event->scenePos().x() - m_startX;
    if(m_bmove)
    {
        ImageItem* firstitem = m_ItemList.first();
        ImageItem* lastitem = m_ItemList.last();
        if(firstitem->x() > EFFECT_START_X  || lastitem->x() < EFFECT_START_X)
        {
            if(firstitem->x() > EFFECT_START_X)
            {
                int i = 0;

                foreach(ImageItem* item,m_ItemList)
                {
                    item->setX(EFFECT_START_X + i * EFFECT_INTERVAL);
                    ++i;
                }
            }
            else if(lastitem->x() < EFFECT_START_X)
            {
                int icount = 0;

                for(int i = m_ItemList.count() - 1; i >= 0; --i)
                {
                    m_ItemList.at(i)->setX(EFFECT_START_X - icount * EFFECT_INTERVAL);
                    ++icount;
                }
            }
            m_bmove = false;
        }
        else
        {
            foreach(ImageItem* item,m_ItemList)
            {
                item->setX(item->x() + imove);
            }
        }
        m_startX = event->scenePos().x();
    }
    else
    {
        if(abs(imove) > 20.0)
        {
            m_startX = event->scenePos().x();
            m_bmove = true;
        }
    }
}
